package com.JUC.cha04.sec02;

import java.util.concurrent.TimeUnit;

public class DeadLockDemo {

    public static void main(String[] args) {

        final Object o1 = new Object();
        final Object o2 = new Object();
        
        new Thread(() -> {
            synchronized (o1){
                System.out.println(Thread.currentThread().getName() + "\t自己有o1锁，想获取o2锁");
                // 暂停几秒钟线程
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (o2){
                    System.out.println(Thread.currentThread().getName() + "\t成功获取o2锁");
                }
            }
        }, "t1").start();

        new Thread(() -> {
            synchronized (o2){
                System.out.println(Thread.currentThread().getName() + "\t自己有o2锁，想获取o1锁");
                // 暂停几秒钟线程
                try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (o1){
                    System.out.println(Thread.currentThread().getName() + "\t成功获取o1锁");
                }
            }
        }, "t2").start();
    }
}
